<?php
$model = new BDynamicFormModel();
$modelClass = get_class($model);
$screen = Blyn::app()->getUIManager()->getCurrentScreen();
$url = $screen->getAjaxLinkUrl('saveEvent', NULL, array('eventId' => $eventId, 'postModel' => $modelClass));
$updateId = $screen->getAjaxContainerId();
?>
<script type="text/javascript">
// this script for collecting the form data and pass to the controller action and doing the on success validations
    function send() {

        var formData = new FormData($("#post-form-2")[0]);
        $.ajax({
            url: '<?php echo $url; ?>',
            type: 'POST',
            data: formData,
            datatype: 'json',
            // async: false,
            beforeSend: function() {
                $("#ajax-loaderdiv").show();
            },
            success: function(html) {
                // on success do some validation or refresh the content div to display the uploaded images 
                jQuery("#<?php echo $updateId; ?>").html(html);
            },
            complete: function() {
                $("#ajax-loaderdiv").hide();
                //$("#refreshIt").click();
            },
            error: function() {
                alert("There may a error on uploading. Try again later");
            },
            cache: false
                    //contentType: false,
                    //processData: false
        });

        return false;
    }
</script>

<?php
$app = Blyn::app()->getActiveApp();
$typeList = $app->getEventTypeList('FullName');
$list = array('0' => '请选择');
$typeList = $list + $typeList;

/**
 * 定义field的名字的时候，一定按照以下规范来定义：
 * 1）下划线作为分隔符
 * 2）第一部分是类型，中间是class，然后是class里面的属性的名字，有时候还可以加一个后缀
 */
$fieldList = array();
$fieldList['eventType'] = $eventTypeField = 'eventType_eventTypeId';
$fieldList['eventName'] = $eventNameField = 'event_name';
$fieldList['eventStartTime'] = $startTimeField = 'event_startTime';
$fieldList['eventEndTime'] = $endTimeField = 'event_endTime';
$fieldList['eventDescription'] = $descriptionField = 'event_description';
$fieldList['eventImage'] = $imageField = 'image_media_image';

$model->addField($eventNameField);
$model->addField($startTimeField);
$model->addField($endTimeField);
$model->addField($descriptionField);
$model->addField($imageField);
$model->addField($eventTypeField);

if ($eventId > 0) {
    $event = new BWEvent($eventId);
    $eventType = $event->getType();
    if ($eventType)
        $model->$eventTypeField = $eventType->getId();
    $model->$eventNameField = $event->name;
    $model->$startTimeField = $event->startTime;
    $model->$endTimeField = $event->endTime;
    $model->$descriptionField = $event->description;
    $media = $event->getMediaManager()->getMedia('image');
    if ($media)
        $model->$imageField = $event->getMediaManager()->getMedia('image')->content;
}
?>
<?php
$form = $this->beginWidget('CActiveForm', array(
    "id" => "post-form-2",
    'enableAjaxValidation' => FALSE,
    'htmlOptions' => array('enctype' => 'multipart/form-data'),
        ));
?>
<div>
    <legend>
        <?php echo $control->getAjaxLink(array('name' => '预定项列表', 'id' => 'showEventList')); ?>
        /添加基本信息
    </legend>
</div>

<div class="tabbable tabs-right" style="margin-top: 15px;"> <!-- Only required for left/right tabs -->
    <ul class="nav nav-tabs" style="margin-bottom: 15px">
        <li class="active"><a href="#tab1" data-toggle="tab">基本信息</a></li>      
        <li class=""><a href="#tab2" data-toggle="tab">场地商信息</a></li>   
    </ul>
    <div class="tab-content">
        <div class="tab-pane active" id="tab1" style="min-height: 380px">                
            <?php
            $control->renderView('event_add_info', array(
                'form' => $form,
                'eventId' => $eventId,
                'model' => $model,
                'fieldList' => $fieldList,
                'typeList' => $typeList
            ));
            ?>  
        </div>  
        <div class="tab-pane " id="tab2" style="min-height: 380px">                
            <?php
            $control->renderView('vender_add_info', array(
                'form' => $form,
                'eventId' => $eventId,
                'model' => $model,
                'fieldList' => $fieldList,
                'typeList' => $typeList
            ));
            ?>  
        </div>      
    </div>
</div>
<?php echo $screen->getAjaxLink('Refresh', array('htmlOptions' => array('id' => 'refreshIt', 'style' => 'display:none'))); ?>
<?php
$htmlOptions = array('class' => 'btn');
if ($eventId == 0) {
    $htmlOptions = array('class' => 'btn disabled');
}
echo $control->getAjaxLink(array('name' => '下一步', 'id' => 'showMessage'), array('parentEventId' => $eventId,
    'messageView' => 'bookable_add',
    'htmlOptions' => $htmlOptions));
?>
<?php
echo '  ';
/*
  echo $control->getAjaxLink(array('name' => 'Show Bookable', 'id' => 'showMessage'), array('parentEventId' => $eventId,
  'messageView' => 'bookable_add',
  'htmlOptions' => array('class' => 'btn')));
 * 
 */
?>
<?php
/*
  echo $screen->getAjaxLink(array('name' => 'Save', 'id' => 'saveEvent'), array(
  'ajaxOptions' => array(
  'type' => 'POST',
  'data' => 'function(){var formData = new FormData($("#post-form-2")[0]);return formData;}',
  //'error' => 'function(data){alert("There may a error on uploading. Try again later");}',
  'cache' => 'false'
  ),
  'htmlOptions' => array(
  'class' => 'btn'
  //'style' => 'display:none'
  )));
 * 
 */
?> 
<?php $this->endWidget(); ?>
<button class="btn"  onclick="javascript:send()">保存</button>